package com.nondo.dean.product.controller.api.app;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.model.param.PurchaseParam;
import com.nondo.dean.product.model.result.ProductResult;
import com.nondo.dean.product.service.PurchaseService;

/**
 * 采购管理APP接口
 * @author zlj
 * @date 2018年4月10日
 */
@RestController("apiAppPurchase")
@RequestMapping("/{v}/app/purchase")
public class PurchaseController {
    
    @Autowired
    PurchaseService purchaseService;
    
    /**
     * 获取产品列表
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/index")
    public View<Map<String,Object>> list(HttpServletRequest request, HttpServletResponse response) {
        View<Map<String,Object>> view = new View<Map<String,Object>>(20000, "获取列表成功");
        Map<String,Object> resultMap = purchaseService.getProductList();
        view.setData(resultMap);
        return view;
    }
    
    /**
     * 分别获取畅销/最新/系列所有产品列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<ProductResult>> listByType(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<PurchaseParam> query) {
        View<Page<ProductResult>> view = new View<Page<ProductResult>>(20000, "获取数据成功");

        PurchaseParam purchaseParam = query.getData();
        if (purchaseParam == null) {
            return View.ofParamsError("请传递要获取列表的类型");
        }

        List<ProductResult> productResultList = this.purchaseService.getList(query.getOffset(), query.getLimit(), purchaseParam);
        
        Page<ProductResult> page = new Page<ProductResult>(query.getPage(), query.getLimit(), this.purchaseService.count(purchaseParam));
        
        page.setContent(productResultList);

        view.setData(page);

        return view;
    }
    
    /**
     * 采购产品搜索结果列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/search")
    public View<Page<ProductResult>> search(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<PurchaseParam> query) {
        View<Page<ProductResult>> view = new View<Page<ProductResult>>(20000, "获取数据成功");

        PurchaseParam purchaseParam = query.getData();
        if (purchaseParam == null || StringUtils.isBlank(purchaseParam.getName())) {
            return View.ofParamsError("请输入要搜索的内容");
        }

        Page<ProductResult> page = new Page<ProductResult>(query.getPage(), query.getLimit(), this.purchaseService.searchListCount(purchaseParam));

        List<ProductResult> productResultList = this.purchaseService.getSearchList(query.getOffset(), query.getLimit(), purchaseParam);
        
        page.setContent(productResultList);

        view.setData(page);

        return view;
    }
    
}